package com.xiaoxie.cloud.controller;

import cn.hutool.core.util.IdUtil;
import com.xiaoxie.cloud.entities.Pay;
import com.xiaoxie.cloud.resp.ResultData;
import com.xiaoxie.cloud.service.PayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

@Slf4j
@RestController
public class PayGateWayController
{
    @Resource
    PayService payService;

    @GetMapping(value = "/pay/gateway/get/{id}")
    public ResultData<Pay> getById(@PathVariable("id") Integer id)
    {
        Pay pay = payService.queryById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "/pay/gateway/info")
    public ResultData<String> getGatewayInfo()
    {
        return ResultData.success("gateway info test："+ IdUtil.simpleUUID());
    }


    @GetMapping("/pay/gateway/filter")
    public ResultData<String> getGatewayFilter(HttpServletRequest request) {
        String result = "";
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headName = headerNames.nextElement();
            String headValue = request.getHeader(headName);
            log.info("请求头名称：{} --- 请求头值：{}",headName,headValue);
            if(headName.equalsIgnoreCase("x-request-id1") || headName.equalsIgnoreCase("x-request-id2")) {
                result = result + headName + " --> " + headValue + " ";
            }
        }
        log.info("===================== 请求参数 =====================");
        String customerId = request.getParameter("customerId");
        log.info("customerId: {}",customerId);
        String name = request.getParameter("customerName");
        log.info("customerName: {}",name);
        log.info("===================== 请求参数 =====================");
        return ResultData.success("getGatewayFilter过滤器test: " + result +" " + IdUtil.simpleUUID());
    }
}
